﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using Things.Common;

namespace Things.ThingsWeb.Components
{
    public partial class BuyerAddress : System.Web.UI.UserControl
    {
        Things.BLL.BuyerAddress bll = new Things.BLL.BuyerAddress();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }

        protected void BindData()
        {
            DataSet ds = new DataSet();
            StringBuilder strWhere = new StringBuilder();
            Things.Model.Buyer currentUser = Session[ConstantString.SessionCurrentBuyerAccount] as Things.Model.Buyer;
            if (currentUser != null)
            {
                strWhere.AppendFormat(" BuyerAccount = '{0}'", currentUser.Account);
                ds = bll.GetList(strWhere.ToString());
                gridView.DataSource = ds;
                gridView.DataBind();
            }
        }

        protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gridView.PageIndex = e.NewPageIndex;
            BindData();
        }

        protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            e.Row.Attributes.Add("style", "background:#FFF");
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton linkbtnDel = (LinkButton)e.Row.FindControl("LinkButton1");
                linkbtnDel.Attributes.Add("onclick", "return confirm(\"你确认要删除吗\")");
            }

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                RadioButton rb = (RadioButton)e.Row.FindControl("RadioButton1");
                if (rb != null && rb.Checked==true)
                {
                    Label Label1 = (Label)e.Row.FindControl("Label1");
                    if (Label1 != null)
                    {
                        hfAddress.Value = Label1.Text;
                    }
                }
            }

            

        }

        protected void gridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string addressID=gridView.DataKeys[e.RowIndex].Value.ToString();
            Guid ID = new Guid(addressID);
            bll.Delete(ID);
            BindData();
        }

        protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
        {
            RadioButton rdb = sender as RadioButton;

            int index = (rdb.NamingContainer as GridViewRow).RowIndex;

            this.gridView.SelectedIndex = index;
            SetRadioButtonChecked(index, gridView);

        }

        protected void SetRadioButtonChecked(int selectedIndex, GridView gridview)
        {
            for (int i = 0; i < gridview.Rows.Count; i++)
            {
                RadioButton radioButton = (RadioButton)gridview.Rows[i].FindControl("RadioButton1");
                if (i == selectedIndex)
                {
                    radioButton.Checked = true;

                    //string addressID = gridview.DataKeys[i].Value.ToString();

                    //Guid addressGUID = Guid.Empty;
                    //if (Guid.TryParse(addressID, out addressGUID) == false)
                    //{
                    //    addressGUID = Guid.Empty;
                    //}
                    //Things.Model.BuyerAddress baModel = bll.GetModel(addressGUID);
                    //if (baModel != null)
                    //{
                    //    hfAddress.Value = baModel.ReceiverName + ", " + baModel.BuyerAddressCity + ", " + baModel.BuyerAddressName + ", " + baModel.ZipCode + ", " + baModel.ReceiverMobile +" "+ baModel.ReceiverTelephone;
                    //}
                    //else
                    //{
                    //    hfAddress.Value = "";
                    //}
                   
                    
                }
                else
                {
                    radioButton.Checked = false;
                }
            }
        }

     

     
  

       
    }
}